package com.da.mybatis.controller;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.da.mybatis.common.BaseController;
import com.da.mybatis.common.Paging;
import com.da.mybatis.common.Result;
import com.da.mybatis.model.Student;
import com.da.mybatis.model.condition.StudentCondition;
import com.da.mybatis.service.StudentService;

/**
 * Controller
 * 
 * @author chenlida
 * @version 1.0.0 2021-05-29
 */
@Api(tags = "student服务")
@RestController
@RequestMapping("/student")
public class StudentController extends BaseController {
    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "根据参数分页查询列表")
    @ApiImplicitParam(name = "condition", value = "查询条件", required = true, dataType = "StudentCondition", paramType = "body")
    @PostMapping("/list")
    public Paging<Student> list(@RequestBody StudentCondition condition) {
        IPage<Student> page = studentService.findStudentByCondition(condition);
        return Paging.buildPaging(page);
    }

    @ApiOperation(value = "根据主键ID查询信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
    @GetMapping(value = "/get/{id}")
    public Result<Student> get(@PathVariable Integer id) {
        if (id == null) {
            return Result.failed("请选择需要查询的数据！");
        }
        Student student = studentService.getStudentById(id);
        return Result.ok(student);
    }

    @ApiOperation(value = "新增信息")
    @ApiImplicitParam(name = "student", value = "", required = true, dataType = "Student", paramType = "body")
    @PostMapping("/add")
    public Result<Student> add(@RequestBody Student student) {
        Boolean bool = studentService.addStudent(student);
        if (bool) {
            return Result.ok(student);
        }
        return Result.failed();
    }

    @ApiOperation(value = "修改信息")
    @ApiImplicitParam(name = "student", value = "", required = true, dataType = "Student", paramType = "body")
    @PutMapping(value = "/update")
    public Result<Boolean> update(@RequestBody Student student) {
        Integer id = student.getId();
        if (id == null) {
            return Result.failed("请选择需要修改的数据！");
        }
        Boolean bool = studentService.updateStudent(student);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID删除")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
    @DeleteMapping(value = "/delete/{id}")
    public Result<Boolean> delete(@PathVariable Integer id) {
        if (id == null) {
            return Result.failed("请选择需要删除的数据！");
        }
        Boolean bool = studentService.deleteStudentById(id);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID列表批量删除")
    @ApiImplicitParam(name = "idList", value = "主键ID列表", required = true, allowMultiple = true, paramType = "body")
    @DeleteMapping("/deleteList")
    public Result<Boolean> deleteList(@RequestBody List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Result.failed("请选择需要删除的数据！");
        }
        Boolean bool = studentService.deleteStudentByIds(idList);
        return Result.okOrFailed(bool);
    }
}